﻿using System;
using System.Configuration;

namespace SPDeployer.Core.Config
{
    public class ContentTypeConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty(Definitions.ContentTypes.ID, IsKey = true, IsRequired = true)]
        public Guid Id
        {
            get { return new Guid(this[Definitions.ContentTypes.ID].ToString()); }
            set { this[Definitions.ContentTypes.ID] = value; }
        }
    }

    [ConfigurationCollection(typeof(ContentTypeConfigurationElement), 
        AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
    public class ContentTypeConfigurationElementCollection : ConfigurationElementCollection
    {
        public ContentTypeConfigurationElement this[int index]
        {
            get { return BaseGet(index) as ContentTypeConfigurationElement; }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ContentTypeConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ContentTypeConfigurationElement)element).Id;
        }
    }
}